 <template>
  <div v-if="visible" class="login-dialog-overlay" @click="handleOverlayClick">
    <div class="login-dialog" @click.stop>
      <div class="login-header">
        <h3>用户登录</h3>
        <span class="close-btn" @click="handleClose">&times;</span>
      </div>
      <div class="login-content">
        <div class="input-group">
          <input type="text" v-model="username" placeholder="请输入手机号">
          <svg-icon name="login" class="svg-icon"></svg-icon>
        </div>
        <div class="input-group">
          <input :type="showPassword ? 'text' : 'password'" v-model="password" placeholder="请输入密码">
          <svg-icon name="passwd" class="svg-icon"></svg-icon>
          <div v-if="password" class="password-toggle" @click="togglePasswordVisibility">
            <svg-icon :name="showPassword ? 'view' : 'view_off'" class="eye-icon"></svg-icon>
          </div>
        </div>
        <div class="login-options">
          <label>
            <input type="checkbox" v-model="rememberMe"> 记住我
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        <button class="login-button" @click="handleLogin">登录</button>
        <div class="register-link">
          还没有账号？<a href="#">立即注册</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import SvgIcon from '@/components/SvgIcon/index.vue'
defineProps<{
  visible: boolean
}>()
const emit = defineEmits(['update:visible'])

const username = ref('')
const password = ref('')
const rememberMe = ref(false)
const showPassword = ref(false)

const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

const handleClose = () => {
  // 清除所有输入内容
  username.value = ''
  password.value = ''
  rememberMe.value = false
  showPassword.value = false
  emit('update:visible', false)
}

const handleOverlayClick = (e: MouseEvent) => {
  if (e.target === e.currentTarget) {
    handleClose()
  }
}

const handleLogin = () => {
  // TODO: 实现登录逻辑
  console.log('登录信息：', {
    username: username.value,
    password: password.value,
    rememberMe: rememberMe.value
  })
}
</script>

<style scoped>
.login-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.login-dialog {
  background: white;
  border-radius: 8px;
  width: 500px;
  max-width: 95%;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  position: absolute;
  top: 30%;
}

.login-header {
  padding: 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login-header h3 {
  margin: 0;
  color: #333;
}

.close-btn {
  font-size: 24px;
  color: #999;
  cursor: pointer;
}

.login-content {
  padding: 30px;
}
.svg-icon{
  width: 20px;
  height: 20px;
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  transition: fill 0.2s;
}

.input-group {
  margin-bottom: 15px;
   width: 100%;
  box-sizing: border-box;
  position: relative;
}

.input-group input:focus {
  outline: none;
  border-color: #4a90e2;
  background-color: #f5f5f585;
}

.input-group input:focus + .svg-icon {
  fill: #4a90e2;
}

.input-group input {
  width: 100%;
  padding: 12px 12px 12px 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  box-sizing: border-box;
  transition: all 0.2s;
  background-color: #f5f5f585;
}


.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 14px;
}

.forgot-password {
  color: #1890ff;
  text-decoration: none;
}

.login-button {
  width: 60%;
  padding: 12px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 15px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
  display: block;
  margin: 0 auto;
}

.login-button:hover {
  background: #40a9ff;
}

.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
}

.register-link a {
  color: #1890ff;
  text-decoration: none;
}

.password-toggle {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.eye-icon {
  width: 20px;
  height: 20px;
  color: #999;
  transition: color 0.2s;
}

.password-toggle:hover .eye-icon {
  color: #4a90e2;
}

.input-group input[type="password"],
.input-group input[type="text"] {
  padding-right: 40px;
}

input[type="password"]::-ms-reveal{
  display: none;
}
input[type="password"]::-ms-clear{
  display: none;
}

</style>
